
// Express requires these dependencies
var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

var app = express();

// Configure our application
app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.favicon());
//  app.use(express.logger(''));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

// Configure error handling
app.configure('development', function(){
  app.use(express.errorHandler());
});

// Setup Routes
app.get('/', routes.index);
app.get('/users', user.list);
app.get('/listen', function (req, res) {
    res.render('listen', { title: 'listener' });
})
// Enable Socket.io
var server = http.createServer(app).listen( app.get('port') );
var io = require('socket.io').listen( server );

// A user connects to the server (opens a socket)
io.sockets.on('connection', function (socket) {
//  socket.on('drawStart', function ( sessionId,x,y ) {
//    console.log('socket: drawStart-x:%s,y:%s',x,y);
//      socket.broadcast.emit( 'drawStart');
//
//  });
//  socket.on('drawEnd', function ( sessionId,x,y ) {
//    console.log('socket: drawEnd-x:%s,y:%s',x,y);
//      socket.broadcast.emit( 'drawEnd');
//
//  });
//
//  socket.on( 'drawLine', function( x,y, session ) {
//
//    console.log( "session " + session + " drew:");
//    console.log( x,y );
//
//    socket.broadcast.emit( 'drawLine', x,y );
//
//  });
//  socket.on( 'colorStyleChange', function( newStyle, session ) {
//
//    console.log( "session " + session + " newStyle:",newStyle);
//    socket.broadcast.emit( 'colorStyleChange', newStyle );
//
//  });
//  socket.on( 'strokeWidthChange', function( newWidth, session ) {
//
//    console.log( "session " + session + " newWidth:",newWidth);
//    socket.broadcast.emit( 'strokeWidthChange', newWidth );
//
//  });
//  socket.on( 'opTypeChange', function( newType, session ) {
//
//    console.log( "session " + session + " newType:",newType);
//    socket.broadcast.emit( 'opTypeChange', newType );
//
//  });
    socket.on( 'reDraw', function( json, session ) {

//        console.log( "session " + session + " newStageJson:",json);
        socket.broadcast.emit( 'reDraw', json );

    });
});